package com.csii.autocode.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 读取模板
 */
public class ReadTemplate {
    private static final Logger logger = LoggerFactory.getLogger(ReadTemplate.class);

    private String templatePath;

    private List<File> templateRootDirs = new ArrayList<>();

    private String stylePath;

    public ReadTemplate(String paramString) {
        logger.debug("----templatePath-----------------" + paramString);
        logger.debug("----stylePath-----------------" + this.stylePath);
        this.templatePath = paramString;
    }

    //传入file获取List
    private void fileTolist(File paramFile) {
        asList(new File[]{paramFile});
    }

    //把数组转化为List集合
    private void asList(File... paramVarArgs) {
        this.templateRootDirs = Arrays.asList(paramVarArgs);
    }

    public String getStylePath() {
        return this.stylePath;
    }

    public void setStylePath(String paramString) {
        this.stylePath = paramString;
    }

    public List<File> getTemplateRootDirs() {
        String str = getClass().getResource(this.templatePath).getFile();
        try {
            str = URLDecoder.decode(str,"utf-8");
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
        }
        logger.debug("-------classpath-------" + str);
        if (str.indexOf("/BOOT-INF/classes!") != -1) {
            str = System.getProperty("user.dir") + File.separator + "config/auto-code/code-template-online/".replace("/", File.separator);
            logger.debug("---JAR--config--classpath-------" + str);
        }
        fileTolist(new File(str));
        return this.templateRootDirs;
    }

    public void setTemplateRootDirs(List<File> paramList) {
        this.templateRootDirs = paramList;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{\"templateRootDirs\":\"");
        stringBuilder.append(this.templateRootDirs);
        stringBuilder.append("\",\"stylePath\":\"");
        stringBuilder.append(this.stylePath);
        stringBuilder.append("\"} ");
        return stringBuilder.toString();
    }
}
